﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using System.Data;
using System.Data.SqlClient;

using System.Text;
using DevExpress.Web.ASPxDataView;
using DAL;
public partial class ResultShow : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Frame.Global.GlobalFunctions.InitComboBoxColumn(gvResultShow.Columns["t_Community_Name"], "Community", "Community_Name", "Community_ID");
            //获取当前登录的用户
            string username = CMSUser.Current.Name;
            if(username!="Administrator")
            {
                cmdCommunityShow.Visible = false;
                lblComminity.Visible = false;
            }
        }
    }
    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        string strSql = @"select convert(char(10),FinishDate,120) as finishdate,* from  dbo.ProjectDetails
                        left join  ( select a.* from ProjectImages a where Images_ID = (select min(Images_ID) from ProjectImages where Project_ID = a.Project_ID) )i	
                        on ProjectDetails.Project_ID=i.Project_ID,Community,Approal,TypeInfo
                        where {0} Community.Community_ID = ProjectDetails.Community_ID 
                        and ProjectDetails.Type_ID = TypeInfo.Type_ID 
                        and Approal.ID=ProjectDetails.ApprovalNumber
                        and Approal.ComminityID = convert(varchar(2),dbo.ProjectDetails.Community_ID)order by Approal.Approal_Date asc";
        StringBuilder sb = new StringBuilder();
        List<SqlParameter> paralist = new List<SqlParameter>();
        if (!string.IsNullOrEmpty(this.txtTime.Text))
        {
            //sb.Append("convert(varchar(10),FinishDate,111)like @ProjectStartDate+'%' and ");
            sb.Append("Approal.Approal_Date like @ProjectStartDate+'%' and ");
            paralist.Add(new SqlParameter("@ProjectStartDate", txtTime.Text));
        }
        if (!string.IsNullOrEmpty(this.cmdCommunityShow.Text))
        {
            sb.Append("ProjectDetails.Community_ID like ''+ @CommunityName+'%' and ");
            paralist.Add(new SqlParameter("@CommunityName", cmdCommunityShow.SelectedItem.Value.ToString()));
        }
        if (!string.IsNullOrEmpty(this.cmdTypeShow.Text))
        {
            sb.Append("TypeInfo.Type_Name = @TypeName and ");
            paralist.Add(new SqlParameter("@TypeName", cmdTypeShow.Text.Trim()));
        }
        if (!string.IsNullOrEmpty(this.cmdApproalShow.Text.Trim()))
        {
            sb.Append("Approal.Approal_ID=@ApprovalNumber and ");
            paralist.Add(new SqlParameter("@ApprovalNumber", cmdApproalShow.Value));
        }
        if (!string.IsNullOrEmpty(this.txtProName.Text.Trim()))
        {
            sb.Append("ProjectDetails.Project_Name like '%'+ @ProjectName +'%' and ");
            paralist.Add(new SqlParameter("@ProjectName",txtProName.Text.Trim()));
        }
        this.gvResultShow.DataSourceID = "";
        string strsql = string.Format(strSql, sb.ToString());
        this.gvResultShow.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(strSql, sb.ToString()), paralist.ToArray());
        this.gvResultShow.DataBind();

    }


    protected void ASPxGridView1_BeforePerformDataSelect(object sender, EventArgs e)
    {
        Session["Project_ID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        this.cmdApproalShow.Text = "";
        this.cmdCommunityShow.Text = "";
        this.cmdTypeShow.Text = "";
        this.txtTime.Text = "";
    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        //获取当前登录的用户
        string username = CMSUser.Current.Name;   
        //查询用户对应的社区
        string CommunityName = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select Communities from Users where UserName='" + username + "'").ToString();
        string strSql;
        strSql = "select * from ProjectDetails where Substring(Community_ID,1, 2)='" + CommunityName + "'";
        DataTable dt = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strSql);
        if (dt.Rows.Count > 0)
        {
            string strSqls = @"select convert(char(10),FinishDate,120) as finishdate,* from  dbo.ProjectDetails
                        left join  ( select a.* from ProjectImages a where Images_ID = (select min(Images_ID) from ProjectImages where Project_ID = a.Project_ID) )i	
                        on 	 ProjectDetails.Project_ID=i.Project_ID,Community,Approal,TypeInfo
                        where Substring(ProjectDetails.Community_ID,1, 2)='" + CommunityName + "' and Community.Community_ID = ProjectDetails.Community_ID and ProjectDetails.Type_ID = TypeInfo.Type_ID and Approal.ID=ProjectDetails.ApprovalNumber and Approal.ComminityID = convert(varchar(2),dbo.ProjectDetails.Community_ID) order by Approal.Approal_Date asc";
            e.Command.CommandText = strSqls;   
        }
        else
        {
            if (!username.Equals("Administrator"))
            {
                string id = "xxx";
                e.Command.CommandText = "select * from ProjectDetails where Community_ID='" +id +"'";
            }

        }
    }

    protected void gvResultShow_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e)
    {

        //if (e.RowType == DevExpress.Web.ASPxGridView.GridViewRowType.Data)
        //{
        //    //e.Row.Attributes.Add("onmouseover", "alert('"+ e.Row.Cells[0].Text +"')");
        //    e.Row.ToolTip = e.Row.Cells[0].Text;
        //}


        //if (e.RowType == DevExpress.Web.ASPxGridView.GridViewRowType.Data)
        //{
        //    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#e3f1d1';");
        //    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
        //}

 //if (e.RowType == DevExpress.Web.ASPxGridView.GridViewRowType.Data)
 //       {
 //           e.Row.Attributes.Add("onmouseover", "alert('"+ ((Label)e.Row.Cells[0].FindControl("Label1")).Text +"')");
            
 //           ((Label)e.Row.Cells[0].FindControl("Label1")).ToolTip = ((Label)e.Row.Cells[0].FindControl("Label1")).Text;
 //       }

    }

    protected void gvResultShow_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "alert('"+ ((Label)e.Row.Cells[0].FindControl("Label1")).Text +"')");
            
            ((Label)e.Row.Cells[0].FindControl("Label1")).ToolTip = ((Label)e.Row.Cells[0].FindControl("Label1")).Text;
        }
    }

}